/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author andhika
 */
public class ServletHitungKalori extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String halaman = request.getParameter("halaman"); //variabel bantu untuk melakukan mapping halaman
        RequestDispatcher rd = null;
        HttpSession session = request.getSession();

        if (halaman.equals("hitungKalori")) {
            rd = request.getRequestDispatcher("hitungKalori.jsp");
            rd.forward(request, response);
        } else if (halaman.equals("hasilKalori")) {
            if (session.getAttribute("username") != null) {
                String temp1 = request.getParameter("nasi");
                String temp2 = request.getParameter("daging");
                String temp3 = request.getParameter("sayuran");
                String temp4 = request.getParameter("gula");
                String temp5 = request.getParameter("buah");


                if (temp1.equals("") || temp2.equals("") || temp3.equals("") || temp4.equals("") || temp5.equals("")) {
                    session.setAttribute("gagal", "semua field harus diisi");
                    rd = request.getRequestDispatcher("hitungKalori.jsp");
                    rd.forward(request, response);
                } else {
                    try {
                        int nasi = Integer.parseInt(temp1);
                        int daging = Integer.parseInt(temp2);
                        int sayuran = Integer.parseInt(temp3);
                        int gula = Integer.parseInt(temp4);
                        int buah = Integer.parseInt(temp5);

                        int hasilKalori = nasi * 10 + daging * 20 + sayuran * 15 + gula * 10 + buah * 10;
                        if (hasilKalori > 5000) {
                            request.setAttribute("hasil", "Kalori yang telah anda konsumsi 2300-2500kkal");
                        } else if (hasilKalori >= 2500 && hasilKalori <= 5000) {
                            request.setAttribute("hasil", "Kalori yang telah anda konsumsi 1700-2100kkal");
                        } else {
                            request.setAttribute("hasil", "Kalori yang telah anda konsumsi 1300-1500kkal");
                        }
                        rd = request.getRequestDispatcher("hasilKalori.jsp");
                        rd.forward(request, response);

                    } catch (NumberFormatException nfe) {
                        session.setAttribute("gagal", "semua field harus angka");
                        rd = request.getRequestDispatcher("hitungKalori.jsp");
                        rd.forward(request, response);
                    }
                }
            } else {
                rd = request.getRequestDispatcher("sudahLogout.jsp");
                rd.forward(request, response);
            }
        }
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
